<?php
namespace Admin\Controller;

class ArticleController extends AdminController {
    /**
     * 文档由两个参量确定，一是分类，二是文档ID，仅凭文档ID是无法定位的.
     *
     * @param null $category_id
     */
    public function index($category_id = null, $pid = null){
        // 获取分类信息
        $category = $this['Category']->info(['id'=>$category_id]);

        // 方法别名跳转
        if ($category['alias']) {
            return redirect(U($category['alias'], ['category_id'=>$category_id, 'pid'=>$pid]));
        }
        $this->assign('category',$category);

        if ( !$category ){
            $this->error('无分类ID或分类不存在');
        }

        if ( !$category['model_id'] ) {
            echo '无模型name';
            exit;
        }

        // 获取该分类的模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);


        // 获取文档列表
        $map = [
            'category_id' => $category['id'],
        ];
        if ($pid) {
            $map['pid'] = $pid;
        }
        $list = $this[$model['name']]->get($map);

        $this->assign('list',$list);

        // 获取文档模板
        if ($model['template_list']) {
            $template = $model['template_list'];
        } else {
            $template = 'index';
        }
        $this->display($template);
    }
    public function add($category_id = null){

        // 当前分类信息
        $category = $this['Category']->info(['id'=>$category_id]);
        if ( !$category ){
            $this->error('无分类ID或分类不存在');
        }

        // 获取当前模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);

        //获取表单字段排序
        $fields = get_model_attribute($model['id']);

        // 初始化信息
        $info = [
            'category_id'=>$category['id'],
            'model_id'  =>  $category['model_id']
        ];

        $this->assign('ADMIN_EDITOR',C('ADMIN_EDITOR'));
        $this->assign('model',$model);
        $this->assign('fields',$fields);
        $this->assign('info',$info);
        $this->assign('title', '新增文档');
        $this->display();
    }

    public function edit($id = null){
        // 当前分类信息
        $category = $this['Category']->info(['id'=>I('category_id')]);
        if ( !I('category_id') || !$category ){
            $this->error('无分类ID或分类不存在');
        }

        $id     =   I('get.id','');
        if(empty($id)){
            $this->error('参数不能为空！');
        }

        // 获取当前模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);

        // 获取文档详情
        $info = $this[$model['name']]->info(['id'=>$id]);

        // 字段排序
        $fields = get_model_attribute($model['id']);

        // 模板传值
        $this->assign('ADMIN_EDITOR',C('ADMIN_EDITOR'));
        $this->assign('info',$info);
        $this->assign('model',$model);
        $this->assign('fields',$fields);
        $this->assign('title', '编辑文档');


        // 不存在则新建
        $this->display();
    }


    /**
     * 更新一条数据
     */
    public function update(){
        // 当前分类信息
        $category = $this['Category']->info(['id'=>I('category_id')]);
        if ( !I('category_id') || !$category ){
            $this->error('无分类ID或分类不存在');
        }

        // 获取当前模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);

        $res = $this[$model['name']]->update();
        if(!$res){
            $this->error($this[$model['name']]->getError());
        }else{
            $this->success($_POST['id']?'更新成功':'新增成功', U('Article/index',['category_id'=>I('category_id'),'pid'=>I('pid',0)]));
        }
    }

    public function delete($id = null){
        $category = $this['Category']->info(['id'=>I('get.category_id')]);
        if ( !I('get.category_id') || !$category ){
            $this->error('无分类ID或分类不存在');
        }

        // 获取当前模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);

        // 有父级模型
        if ($parent_model = $this['Model']->info(['pid'=>$model['id']])) {
            $this[$parent_model['name']]->remove(null, $id);
        }

        $id = array_unique((array)$id);
        $res = $this[$model['name']]->remove($id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败 '.$this['Config']->getError());
        }
    }

    public function createUrl($url = null)
    {
        if ($url) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://dwz.wailian.work/api.php");
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_REFERER, 'dwz.wailian.work');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $data = array('url' => 'aHR0cDovL3ZvdGUuaHVpeW91emFuLmNvbQ==','site' => 'sina');
            dump($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $strRes = curl_exec($ch);
            curl_close($ch);
            $arrResponse = json_decode($strRes, true);
            dump($arrResponse);
            if ($arrResponse['status'] == 0) {
                /**错误处理*/
                echo iconv('UTF-8', 'GBK', $arrResponse['err_msg']) . "\n";
            }
            /** tinyurl */
            echo $arrResponse['tinyurl'] . "\n";
        } else {
            echo '无url';
        }
    }

}